﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DBLib_CMDB
{
    public class DBFactory
    {
        private static Hashtable _ConnectString = new Hashtable();

        public static void InitConnectString(string connectString)
        {
            InitConnectString("Default", connectString);
        }

        public static void InitConnectString(string connectKey, string connectString)
        {
            if (!_ConnectString.ContainsKey(connectKey))
                _ConnectString.Add(connectKey, connectString);
        }

        public static SqlConnection getConnect(string connectKey)
        {
            SqlConnection conn = null;
            try
            {
                if (_ConnectString.ContainsKey(connectKey))
                {
                    string mm = _ConnectString[connectKey].ToString();
                    conn = new SqlConnection(_ConnectString[connectKey].ToString());
                    conn.Open();
                }
            }
            catch (Exception ex)
            {
                log4net.Logger.Error("Get Connection Failed. Error messsage : " + ex.Message);
                return null;
            }
            return conn;
        }

        public static SqlConnection getConnect()
        {
            return getConnect("Default");
        }
    }
}
